package com.tanhua.dubbo.api.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.api.TestSoulApi;
import com.tanhua.dubbo.pojo.Report;
import com.tanhua.dubbo.pojo.SimpleReport;
import com.tanhua.dubbo.pojo.SoulQuestions;
import com.tanhua.dubbo.pojo.UserSoulTestStatus;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Map;

@DubboService(version = "1.0.0")
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询测灵魂的试题
     * @param questionLevel 等级：2初级，3中级，5高级
     * @return 试题集合
     */
    @Override
    public List<SoulQuestions> querySoulQuestions(Integer questionLevel) {
        return this.mongoTemplate.find(new Query(Criteria.where("questionLevel").is(questionLevel)), SoulQuestions.class);
    }

    /**
     * 查询用户答题状态
     * @param userId 用户userId
     * @return 状态数组{0,1,1} 分别为初、中、高级答题状态，0代表可作答，1代表不可作答
     */
    @Override
    public Integer[] queryUserSoulTestStatus(Long userId) {
        UserSoulTestStatus userSoulTestStatus = this.mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)), UserSoulTestStatus.class);
        if (null == userSoulTestStatus) {
            return this.mongoTemplate.save(new UserSoulTestStatus(ObjectId.get(),userId,new Integer[]{0,1,1})).getStatus();
        }
        return userSoulTestStatus.getStatus();
    }

    /**
     * 查询报告
     * @param reportId
     * @return
     */
    @Override
    public Report queryReportByReportId(String reportId) {
        return this.mongoTemplate.findOne(Query.query(Criteria.where("reportId").is(reportId)), Report.class);
    }

    /**
     * 更新report表中的”与我相似“字段
     * @param reportId
     * @param userId
     * @param logo
     * @return
     */
    @Override
    public void updateReportInSimilarYou(String reportId, Long userId, String logo) {
        //判断该字段中是否已经存在自己
        Report report = this.queryReportByReportId(reportId);//先查询报告
        List<Map<Object, Object>> similarYou = report.getSimilarYou();//拿到”与我相似“字段
        for (Map<Object, Object> map : similarYou) {
            if (ObjectUtil.equal(Convert.toLong(map.get("id")), userId)) {//如果存在，不更新
                return;
            }
        }

        //更新report表中的”与我相似“字段
        Query query = Query.query(Criteria.where("reportId").is(reportId));
        similarYou.add(MapUtil.builder().put("id", userId).put("avatar", logo).build());
        Update update = Update.update("similarYou", similarYou);
        this.mongoTemplate.updateFirst(query, update, Report.class);
    }

    /**
     * 更新用户答题状态表
     * @param userId
     * @param status
     */
    @Override
    public void updateUserSoulTestStatus(Long userId, Integer[] status) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        Update update = Update.update("status", status);
        this.mongoTemplate.updateFirst(query, update, UserSoulTestStatus.class);
    }

    /**
     * 查询报告通过userId
     * @param userId
     * @return
     */
    @Override
    public Report queryReportByUserId(Long userId) {
        Query query = Query.query(Criteria.where("id").is(userId));
        return this.mongoTemplate.findOne(query, Report.class);
    }

    /**
     * 将简单信息存到report-user关系表中
     * @param userId
     * @param reportId
     * @param value
     */
    @Override
    public void upsertSimpleReport(Long userId, String reportId, Integer value) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("questionLevel").is(value));
        Update update = Update.update("reportId", reportId);
        this.mongoTemplate.upsert(query,update, SimpleReport.class);
    }

    /**
     * 通过userId和问卷等级查询报告
     * @param userId
     * @param value
     * @return
     */
    @Override
    public SimpleReport querySimpleReport(Long userId, Integer value) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("questionLevel").is(value));
        return this.mongoTemplate.findOne(query, SimpleReport.class);
    }
}
